const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 500,
    indicatorDots:true,
    circular:true,
    page:1,
    limit:10,
    dataList:[],
    noData:true,
    lastTime: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      windowHeight: app.globalData.windowHeight
    })
    this.getCardList();
    this.bannerList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      lastTime: 0
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.setData({
        page:1
      })
      wx.showLoading({
        title: '刷新中',
      })
      this.getCardList();
      this.bannerList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log("222222");
    var that = this;
    var page = this.data.page;
    var total_page= this.data.total_page;
    
    if (total_page > page){
      this.setData({
        page: page+1
      })
      wx.showLoading({
        title: '加载中',
      })
      this.getCardList();
    }else{
      this.setData({
        noData:false
      })
    }
    
  },
  getCardList:function(){
    var params = new Object()
    var that = this;
    params.page=this.data.page;
    params.limit = this.data.limit;
    if (this.data.cid != undefined){
      params.cid = this.data.cid;
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'coupons/cateList',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          if (res.data.data.paging.page==1){
            that.setData({
              dataList:[]
            })
          }
          var dataList = that.data.dataList.concat(res.data.data.list);
          that.setData({
            dataList: dataList,
            cid: res.data.data.cid,
            total_page: res.data.data.paging.total_page,
            cat_list: res.data.data.cat_list
          })
        }
        wx.stopPullDownRefresh();
        wx.hideLoading()
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
        wx.stopPullDownRefresh();
        wx.hideLoading()
      }
    })
  },
  bannerList:function(){
    var params = new Object();
    params.type=1;
    var that = this;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'user/banner',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            bannerList:res.data.data
          })
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  selectCid:function(e){
    // console.log(e);
    var cid = e.currentTarget.dataset.cid;
    this.setData({
      page:1,
      cid:cid,
      noData:true
    })
    // wx.showLoading({
    //   title: '加载中',
    // })
    this.getCardList();
  },
  //查看详情
  cardDetails: function (e) {

    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../cardDetail/cardDetail?id=' + id
    })
  },
  cardLink:function(){
    wx.navigateTo({
      url: '../cardMy/cardMy'
    })
  }
})